/**
 * f-club.cn
 * Copyright (c) 2009-2012 All Rights Reserved.
 */
package com.mtoolkit.mybatis;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.ibatis.cache.Cache;

import com.mtoolkit.cache.CacheDriver;

/**
 * Mybatis cache adapter.
 * 
 * @author michael
 * @version $Id: OsCache.java, v 0.1 2012-10-17 上午10:00:55 michael Exp $
 */
public class MybatisCacheAdapter implements Cache {
    
    private final String        _id;
    private final CacheDriver   _delegate;
    private final ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
    
    public MybatisCacheAdapter(String id, CacheDriver delegate) {
        _id = id;
        _delegate = delegate;
    }
    
    @Override
    public String getId() {
        return _id;
    }

    @Override
    public int getSize() {
        return 0;
    }

    @Override
    public void putObject(Object key, Object value) {
        _delegate.put(key2String(key), value);
    }

    @Override
    public Object getObject(Object key) {
        return _delegate.get(key2String(key));
    }

    @Override
    public Object removeObject(Object key) {
        Object value = getObject(key);
        if (value != null) {
            _delegate.remove(key2String(key));
        }
        return value;
    }

    @Override
    public void clear() {
        _delegate.clear();
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return _readWriteLock;
    }

    private String key2String(Object key) {
        return key.toString();
    }
    
}
